package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.Discount;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LeTuGolf
 * package：com.tongyu.luck.paradisegolf.adapter
 * email：893855882@qq.com
 * data：16/7/27
 */
public class UploadDiscountAdapter extends
        RecyclerView.Adapter<UploadDiscountAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;
    private List<Discount> list = new ArrayList<>();

    public UploadDiscountAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }


    public void setList(List<Discount> list) {
        this.list = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_remove;
        EditText et_discount, et_number;

        public ViewHolder(View view) {
            super(view);
            iv_remove = (ImageView) view.findViewById(R.id.iv_remove);
            et_discount = (EditText) view.findViewById(R.id.et_discount);
            et_number = (EditText) view.findViewById(R.id.et_number);
        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.discount_list_item,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(viewHolder.getAdapterPosition(), v);
                }
            });
        }
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        Discount discount = list.get(position);
        viewHolder.et_number.setText(discount.getCount());
        viewHolder.et_discount.setText(discount.getDiscount());
        viewHolder.iv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
